<?php
namespace app\admin\controller\system;


use app\common\controller\AdminController;
use think\App;
use think\facade\View;
use think\Request;
/**
 * Class Menu
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="菜单管理",auth=true)
 */
class Menu extends AdminController
{

    use \app\common\traits\Curd;
    

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = app('app\common\model\system\SystemMenu');
        $this->order = 'sort asc';
    }

    /**/
    public function navigate(){


        $data = [
            [
                'path' => "/" ,
                'component' => "Layout" ,
                'redirect' => "index" ,
                'children' => [
                    [
                        'path' => "index" ,
                        'name' => "Index" ,
                        'component' => "views/index/index" ,
                        'meta' => [
                            'title' => "首页" ,
                            'icon' => "home" ,
                            'affix' => true ,
                        ]
                    ]
                ]
            ]];


        $data = [
            'code' => 200,
            'msg' => '正在请求中...',
            'data' => $data,
        ];
        return json($data);

    }

    public function login(){
        $data = [
            'accessToken'=>'admin-accessToken',

        ];
        $data = [
            'code' => 200,
            'msg' => '正在请求中...',
            'data' => $data,
        ];
        return json($data);
    }

    public function userInfo(){

        $data = [
          'avatar'=>'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif',
          'permissions'=>['admin'],
          'username'=>'admin'
        ];
        $data = [
            'code' => 200,
            'msg' => '正在请求中...',
            'data' => $data,
        ];
        return json($data);
    }

    /**
     * 获取附带数据
     * @param Request $request
     * @return mixed
     */
    public function otherData($request)
    {
        $otherData = $this->model->with(['childs'=>function($query){
            $query->where(['status'=>1,'type'=>1])->order('sort asc')->with(['childs'=>function($query){
                $query->where(['status'=>1,'type'=>1])->order('sort asc')->select();
            }]);
        }
        ])->where(['pid'=>0,'status'=>1])->order('sort asc')->select();

        View::assign('otherData',$otherData);
    }

    public function getData()
    {
        if (method_exists($this, 'get_where')) {
            $where = $this->get_where($this->request->param() ?? []);
            $this->model = $this->model->where($where);
        }
        if (method_exists($this, 'get_with')) {
            $with = $this->get_with();
            $this->model = $this->model->with($with);
        }
        $this->request->filter(['strip_tags']);

        $res = $this->model->field($this->field ?? '*')
            ->order($this->order)->paginate($this->request->param('limit', 1000));

        // $res = $model->order('id','desc')->paginate($request->param('limit',30));

        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => $res->total(),
            'data' => $res->items(),
        ];
        return json($data);
    }
    /**
     * 获取验证规则
     * @param $post
     * @return array
     */
    public function get_check_rule($post)
    {
            $rule = [
                'pid|上级菜单'   => 'require',
                'title|菜单名称' => 'require',
//                'icon|菜单图标'  => 'require',
                'href|菜单url'  => 'require|unique:system_menu,href,'.$post['id'].',id',
            ];

        return $rule;
    }






}